/**
 * 
 */
package org.eichar.server.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Jesse
 * 
 */
public class LoggingStatementDecorator implements Statement {

    private final Statement wrapped;

    private final Logger logger;

    private final Level level;

    public LoggingStatementDecorator(Statement statement, Logger logger,
            Level level) {
        this.wrapped = statement;
        this.logger = logger;
        this.level = level;
    }

    /**
     * @see java.sql.Statement#addBatch(java.lang.String)
     */
    public void addBatch(String arg0) throws SQLException {
        logger.log(level, "Adding batch statement: " + arg0); //$NON-NLS-1$
        wrapped.addBatch(arg0);
    }

    /**
     * @see java.sql.Statement#cancel()
     */
    public void cancel() throws SQLException {
        logger.log(level, "cancelling statement"); //$NON-NLS-1$
        wrapped.cancel();
    }

    /**
     * @see java.sql.Statement#clearBatch()
     */
    public void clearBatch() throws SQLException {
        logger.log(level, "clearBatch()"); //$NON-NLS-1$
        wrapped.clearBatch();
    }

    /**
     * @see java.sql.Statement#clearWarnings()
     */
    public void clearWarnings() throws SQLException {
        logger.log(level, "clear Warnings"); //$NON-NLS-1$
        wrapped.clearWarnings();
    }

    /**
     * @see java.sql.Statement#close()
     */
    public void close() throws SQLException {
        logger.log(level, "statement closed"); //$NON-NLS-1$
        wrapped.close();
    }

    /**
     * @see java.sql.Statement#execute(java.lang.String, int)
     */
    public boolean execute(String arg0, int arg1) throws SQLException {
        logger.log(level, "executing: " + arg0 + " with arg: " + arg1); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.execute(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#execute(java.lang.String, int[])
     */
    public boolean execute(String arg0, int[] arg1) throws SQLException {
        StringBuilder args = new StringBuilder();
        for (int i : arg1) {
            args.append(i);
            args.append(","); //$NON-NLS-1$
        }
        logger.log(level, "executing " + arg0 + " with args: " + args); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.execute(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
     */
    public boolean execute(String arg0, String[] arg1) throws SQLException {
        StringBuilder args = new StringBuilder();
        for (String i : arg1) {
            args.append(i);
            args.append(","); //$NON-NLS-1$
        }
        logger.log(level, "executing " + arg0 + " with args: " + args); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.execute(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#execute(java.lang.String)
     */
    public boolean execute(String arg0) throws SQLException {
        logger.log(level, "executing " + arg0); //$NON-NLS-1$
        return wrapped.execute(arg0);
    }

    /**
     * @see java.sql.Statement#executeBatch()
     */
    public int[] executeBatch() throws SQLException {
        logger.log(level, "executing batch requests"); //$NON-NLS-1$
        return wrapped.executeBatch();
    }

    /**
     * @see java.sql.Statement#executeQuery(java.lang.String)
     */
    public ResultSet executeQuery(String arg0) throws SQLException {
        logger.log(level, "execute query: " + arg0); //$NON-NLS-1$
        return wrapped.executeQuery(arg0);
    }

    /**
     * @see java.sql.Statement#executeUpdate(java.lang.String, int)
     */
    public int executeUpdate(String arg0, int arg1) throws SQLException {
        logger.log(level, "execute update" + arg0 + " with arg: " + arg1); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.executeUpdate(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#executeUpdate(java.lang.String, int[])
     */
    public int executeUpdate(String arg0, int[] arg1) throws SQLException {
        StringBuilder args = new StringBuilder();
        for (int i : arg1) {
            args.append(i);
            args.append(","); //$NON-NLS-1$
        }
        logger.log(level, "executing " + arg0 + " with args: " + args); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.executeUpdate(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#executeUpdate(java.lang.String,
     *      java.lang.String[])
     */
    public int executeUpdate(String arg0, String[] arg1) throws SQLException {
        StringBuilder args = new StringBuilder();
        for (String i : arg1) {
            args.append(i);
            args.append(","); //$NON-NLS-1$
        }
        logger.log(level, "executing update " + arg0 + " with args: " + args); //$NON-NLS-1$ //$NON-NLS-2$
        return wrapped.executeUpdate(arg0, arg1);
    }

    /**
     * @see java.sql.Statement#executeUpdate(java.lang.String)
     */
    public int executeUpdate(String arg0) throws SQLException {
        logger.log(level, ""); //$NON-NLS-1$
        return wrapped.executeUpdate(arg0);
    }

    /**
     * @see java.sql.Statement#getConnection()
     */
    public Connection getConnection() throws SQLException {
        return wrapped.getConnection();
    }

    /**
     * @see java.sql.Statement#getFetchDirection()
     */
    public int getFetchDirection() throws SQLException {
        return wrapped.getFetchDirection();
    }

    /**
     * @see java.sql.Statement#getFetchSize()
     */
    public int getFetchSize() throws SQLException {
        return wrapped.getFetchSize();
    }

    /**
     * @see java.sql.Statement#getGeneratedKeys()
     */
    public ResultSet getGeneratedKeys() throws SQLException {
        return wrapped.getGeneratedKeys();
    }

    /**
     * @see java.sql.Statement#getMaxFieldSize()
     */
    public int getMaxFieldSize() throws SQLException {
        return wrapped.getMaxFieldSize();
    }

    /**
     * @see java.sql.Statement#getMaxRows()
     */
    public int getMaxRows() throws SQLException {
        return wrapped.getMaxRows();
    }

    /**
     * @see java.sql.Statement#getMoreResults()
     */
    public boolean getMoreResults() throws SQLException {
        return wrapped.getMoreResults();
    }

    /**
     * @see java.sql.Statement#getMoreResults(int)
     */
    public boolean getMoreResults(int arg0) throws SQLException {
        return wrapped.getMoreResults(arg0);
    }

    /**
     * @see java.sql.Statement#getQueryTimeout()
     */
    public int getQueryTimeout() throws SQLException {
        return wrapped.getQueryTimeout();
    }

    /**
     * @see java.sql.Statement#getResultSet()
     */
    public ResultSet getResultSet() throws SQLException {
        return wrapped.getResultSet();
    }

    /**
     * @see java.sql.Statement#getResultSetConcurrency()
     */
    public int getResultSetConcurrency() throws SQLException {
        return wrapped.getResultSetConcurrency();
    }

    /**
     * @see java.sql.Statement#getResultSetHoldability()
     */
    public int getResultSetHoldability() throws SQLException {
        return wrapped.getResultSetHoldability();
    }

    /**
     * @see java.sql.Statement#getResultSetType()
     */
    public int getResultSetType() throws SQLException {
        return wrapped.getResultSetType();
    }

    /**
     * @see java.sql.Statement#getUpdateCount()
     */
    public int getUpdateCount() throws SQLException {
        return wrapped.getUpdateCount();
    }

    /**
     * @see java.sql.Statement#getWarnings()
     */
    public SQLWarning getWarnings() throws SQLException {
        return wrapped.getWarnings();
    }

    /**
     * @see java.sql.Statement#setCursorName(java.lang.String)
     */
    public void setCursorName(String arg0) throws SQLException {
        logger.log(level, "set cursor name to " + arg0); //$NON-NLS-1$
        wrapped.setCursorName(arg0);
    }

    /**
     * @see java.sql.Statement#setEscapeProcessing(boolean)
     */
    public void setEscapeProcessing(boolean arg0) throws SQLException {
        logger.log(level, "set escape processing: " + arg0); //$NON-NLS-1$
        wrapped.setEscapeProcessing(arg0);
    }

    /**
     * @see java.sql.Statement#setFetchDirection(int)
     */
    public void setFetchDirection(int arg0) throws SQLException {
        logger.log(level, "set Fetch direction: " + arg0); //$NON-NLS-1$
        wrapped.setFetchDirection(arg0);
    }

    /**
     * @see java.sql.Statement#setFetchSize(int)
     */
    public void setFetchSize(int arg0) throws SQLException {
        logger.log(level, "set fetch size: " + arg0); //$NON-NLS-1$
        wrapped.setFetchSize(arg0);
    }

    /**
     * @see java.sql.Statement#setMaxFieldSize(int)
     */
    public void setMaxFieldSize(int arg0) throws SQLException {
        logger.log(level, "set max field size: " + arg0); //$NON-NLS-1$
        wrapped.setMaxFieldSize(arg0);
    }

    /**
     * @see java.sql.Statement#setMaxRows(int)
     */
    public void setMaxRows(int arg0) throws SQLException {
        logger.log(level, "set max rows: " + arg0); //$NON-NLS-1$
        wrapped.setMaxRows(arg0);
    }

    /**
     * @see java.sql.Statement#setQueryTimeout(int)
     */
    public void setQueryTimeout(int arg0) throws SQLException {
        logger.log(level, "set Query Timeout: " + arg0); //$NON-NLS-1$
        wrapped.setQueryTimeout(arg0);
    }

}
